package com.aaa.config;

import com.aaa.entity.Staff;
import com.aaa.service.StaffService;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.security.auth.login.AccountNotFoundException;
import java.util.ArrayList;
import java.util.List;

@Component
public class UserConfig implements UserDetailsService {
    @Resource
    private StaffService staffService;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        //1 根据账号获取数据库是否有记录
        Staff staff=staffService.login(username);
        //2 根据账号查询数据库获取当前用户的权限信息
       /* List<SimpleGrantedAuthority> list = new ArrayList<>();
        SimpleGrantedAuthority authority = null;
        if(username.equals("管理员")){
            authority = new SimpleGrantedAuthority("ROLE_ADMIN");
        }else{
            authority = new SimpleGrantedAuthority("ROLE_TEST");
        }
        list.add(authority);
        //把角色信息放入到当前登录用户中
        staff.setAuthorities(list);
        if(staff==null){
            try {
                throw new AccountNotFoundException("账号没找到");
            } catch (AccountNotFoundException e) {
                e.printStackTrace();
            }
        }*/
        //3 返回用户信息
        return staff;
    }
}
